package org.aphelie.torg.ruler.internal.core.text.character;

import org.aphelie.torg.ruler.core.character.ICharacterAttribute;
import org.aphelie.torg.ruler.core.character.ICharacterElement;
import org.aphelie.torg.ruler.core.character.ICharacterExtension;
import org.aphelie.torg.ruler.core.character.ICharacterLibrary;
import org.aphelie.torg.ruler.core.character.ICharacterModelFactory;
import org.aphelie.torg.ruler.core.character.ICharacterObject;
import org.aphelie.torg.ruler.core.character.ICharacterSketch;
import org.aphelie.torg.ruler.internal.core.text.DocumentTextNode;
import org.aphelie.torg.ruler.internal.core.text.IDocumentAttributeNode;
import org.aphelie.torg.ruler.internal.core.text.IDocumentElementNode;
import org.aphelie.torg.ruler.internal.core.text.IDocumentNodeFactory;
import org.aphelie.torg.ruler.internal.core.text.IDocumentTextNode;
import org.eclipse.core.runtime.CoreException;


public class CharacterDocumentNodeFactory implements ICharacterModelFactory, IDocumentNodeFactory {

    private CharacterModelBase fModel;

    public CharacterDocumentNodeFactory(CharacterModelBase model) {
        fModel = model;
    }

    public IDocumentElementNode createDocumentNode(String name, IDocumentElementNode parent) {
        if (parent == null)
            return createCharacterBase(name);

        if (parent instanceof CharacterBaseNode) {
            if ("extension".equals(name)) //$NON-NLS-1$
                return (IDocumentElementNode) createExtension();
            //if ("extension-point".equals(name)) //$NON-NLS-1$
              //  return (IDocumentElementNode) createExtensionPoint();
        } else {
            if (name.equals("import") && parent instanceof CharacterElementNode) { //$NON-NLS-1$
                if (((CharacterElementNode) parent).getName().equals("requires")) { //$NON-NLS-1$
                    IDocumentElementNode ancestor = parent.getParentNode();
                    /* TODO 
                    if (ancestor != null && ancestor instanceof CharacterBaseNode) {
                        return (IDocumentElementNode) createImport();
                    }
                    */
                }
            } else if (name.equals("library") && parent instanceof CharacterElementNode) { //$NON-NLS-1$
                if (((CharacterElementNode) parent).getName().equals("runtime")) { //$NON-NLS-1$
                    IDocumentElementNode ancestor = parent.getParentNode();
                    /* TODO 
                    if (ancestor != null && ancestor instanceof CharacterBaseNode) {
                        return (IDocumentElementNode) createLibrary();
                    }
                    */
                }
            }
        }
        IDocumentElementNode node = (IDocumentElementNode) createElement((ICharacterObject) parent);
        node.setXMLTagName(name);
        return node;
    }

    public IDocumentAttributeNode createAttribute(String name, String value, IDocumentElementNode enclosingElement) {
        CharacterAttribute attribute = new CharacterAttribute();
        try {
            attribute.setName(name);
            attribute.setValue(value);
        } catch (CoreException e) {
        }
        attribute.setEnclosingElement(enclosingElement);
        attribute.setModel(fModel);
        attribute.setInTheModel(true);
        return attribute;
    }

    private CharacterBaseNode createCharacterBase(String name) {
        return (CharacterBaseNode) fModel.createCharacterBase(name.equals("fragment")); //$NON-NLS-1$

    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterModelFactory#createImport()
     */
    /* TODO
    public ICharacterImport createImport() {
        CharacterImportNode node = new CharacterImportNode();
        node.setModel(fModel);
        node.setXMLTagName("import"); //$NON-NLS-1$
        return node;
    }
    */
    
    /* TODO
    public ICharacterImport createImport(String pluginId) {
        CharacterImportNode node = new CharacterImportNode(pluginId);
        node.setModel(fModel);
        node.setXMLTagName("import"); //$NON-NLS-1$
        return node;
    }
    */

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterModelFactory#createLibrary()
     */

    public ICharacterLibrary createLibrary() {
        CharacterLibraryNode node = new CharacterLibraryNode();
        node.setModel(fModel);
        node.setXMLTagName("library"); //$NON-NLS-1$
        return node;
    }

    public ICharacterSketch createSketch() {

        CharacterSketchNode node = new CharacterSketchNode();
        node.setModel(fModel);
        node.setXMLTagName("library"); //$NON-NLS-1$
        return node;
    }
 
    

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.IExtensionsModelFactory#createAttribute(org.eclipse.pde.core.plugin.ICharacterElement)
     */
    public ICharacterAttribute createAttribute(ICharacterElement element) {
        return null;
    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.IExtensionsModelFactory#createElement(org.eclipse.pde.core.plugin.ICharacterObject)
     */
    public ICharacterElement createElement(ICharacterObject parent) {
        CharacterElementNode node = new CharacterElementNode();
        node.setModel(fModel);
        return node;
    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.IExtensionsModelFactory#createExtension()
     */
    public ICharacterExtension createExtension() {
        CharacterExtensionNode node = new CharacterExtensionNode();
        node.setModel(fModel);
        node.setXMLTagName("extension"); //$NON-NLS-1$
        return node;
    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.IExtensionsModelFactory#createExtensionPoint()
     */
    /* TODO
    public ICharacterExtensionPoint createExtensionPoint() {
        CharacterExtensionPointNode node = new CharacterExtensionPointNode();
        node.setModel(fModel);
        node.setXMLTagName("extension-point"); //$NON-NLS-1$
        return node;
    }
    */

    /* (non-Javadoc)
     * @see org.eclipse.pde.internal.core.text.IDocumentNodeFactory#createDocumentTextNode(java.lang.String, org.eclipse.pde.internal.core.text.IDocumentElementNode)
     */
    public IDocumentTextNode createDocumentTextNode(String content, IDocumentElementNode parent) {
        DocumentTextNode textNode = new DocumentTextNode();
        textNode.setEnclosingElement(parent);
        parent.addTextNode(textNode);
        textNode.setText(content.trim());
        return textNode;
    }
}
